package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.*;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 我的收藏post请求
 * @author david
 */
public class PostMyFavoritesLIst extends BaseCommand {


    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private String TAG = "PostMyFavoritesLIst";
    private Class c;
    private String catid;
    private String  userid ;
    private int mid;
    private String aciton = "fav";
    private String key;

    public PostMyFavoritesLIst(int mid ,String catid, String userid, Class<?> c, int page,String key) {
        this.catid = catid;
        this.userid = userid;
        this.c = c;
        this.page = page;
        this.mid = mid;
        this.key = key;
    }
    public PostMyFavoritesLIst(int mid ,String catid, String userid, Class<?> c, int page) {
        this.catid = catid;
        this.userid = userid;
        this.c = c;
        this.page = page;
        this.mid = mid;
        this.key = key;
    }

    public PostMyFavoritesLIst(int mid , String userid, Class<?> c, int page) {
        this.userid = userid;
        this.c = c;
        this.page = page;
        this.mid = mid;
    }

    public PostMyFavoritesLIst(String catid, String userid, Class<?> c, int page) {
        this.catid = catid;
        this.userid = userid;
        this.c = c;
        this.page = page;
    }

    public PostMyFavoritesLIst(String catid, String userid, Class<?> c) {
        this.catid = catid;
        this.userid = userid;
        this.c = c;
    }


    @Override
    public String getRequestUrl() {
        return DataCenter.POST_Favorites_USER_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        if(!"".equals(catid)&&null!=catid){
            paramters.put("catid", catid  + "");
        }
        if(mid!=0){
            paramters.put("mid", mid + "");
        }
        paramters.put("userid",userid);
        paramters.put("action", aciton);
        paramters.put("page",page + "");
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "PostMyFavoritesLIst.yes()");
        Log.d("==-=-=",key+"-=-=-=1111");
        try {
            Log.d(TAG, result.object.toString());
            String resultStr = result.object.toString();
            Log.d("Myfavorites typeid = ", catid+"--------"+ resultStr);
            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray jsonArray = jsonObject.optJSONArray("data");
            if (page == DataCenter.PAGE) {
                Log.d("==-=-=",key+"-=-=-=22222");
                if("66".equals(key)){
                    Log.d("==-=-=","-=-=-=1");
                    for (int i = DataCenter.auctionArrayList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.auctionArrayList.remove(i);
                    }
                }else if("4".equals(key)){
                    Log.d("==-=-=","-=-=-=2");
                    for (int i = DataCenter.daydayCouponBeanList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.daydayCouponBeanList.remove(i);
                    }
                }else if("32".equals(key)){
                    Log.d("==-=-=","-=-=-=3");
                    for (int i = DataCenter.playBeanList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.playBeanList.remove(i);
                    }
                }else if("56".equals(key)){
                    Log.d("==-=-=","-=-=-=4");
                    for (int i = DataCenter.messageInfoList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.messageInfoList.remove(i);
                    }
                }
                else if("64".equals(key)){
                    Log.d("==-=-=","-=-=-=5");
                    for (int i = DataCenter.secondHandCar.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.secondHandCar.remove(i);
                    }
                }else if("65".equals(key)){
                    Log.d("==-=-=","-=-=-=6");
                    for (int i = DataCenter.secondHandHouses.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.secondHandHouses.remove(i);
                    }
                }else if("16".equals(key)){
                    Log.d("==-=-=","-=-=-=7");
                    for (int i = DataCenter.playBeanList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.playBeanList.remove(i);
                    }
                }else if("49".equals(key)){
                    Log.d("==-=-=","-=-=-=8");
                    for (int i = DataCenter.playBeanList.size()-1; i >= (DataCenter.myfav_position/20+1)*20-20; i--) {
                        DataCenter.playBeanList.remove(i);
                    }
                }

            }
            ArrayList<DaydayCouponBean> ddList = DataCenter.daydayCouponBeanList;
            ArrayList<Auction> auctionList = DataCenter.auctionArrayList;
            ArrayList<MessageBean> messageList = DataCenter.messageInfoList;
            ArrayList<SecondHandCar> secondHandCarList = DataCenter.secondHandCar;
            ArrayList<SecondHandHouse> secondHandHouseList = DataCenter.secondHandHouses;
            ArrayList<PlayBean> playList = DataCenter.playBeanList;
            ArrayList<FocusOnBean> focusOnBean = DataCenter.focusOnBeans;
            ArrayList<MyJoinBean> myJoinBeans = DataCenter.myJoinBeans;

            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                ddList.clear();
                auctionList.clear();
                messageList.clear();
                secondHandCarList.clear();
                secondHandHouseList.clear();
                playList.clear();
                focusOnBean.clear();
                myJoinBeans.clear();
            }
            for(int i = 0; i<jsonArray.length();++i){
                JSONObject o =jsonArray.getJSONObject(i);
                if(catid.equals("56")){
                    MessageBean bean  = (MessageBean) JsonUtil.jsonToObject(o.toString(), c);
                    messageList.add(bean);
                }else if (catid.equals("66")){
                    Auction bean = (Auction) JsonUtil.jsonToObject(o.toString(),c);
                    auctionList.add(bean);
                }else if (catid.equals("65")){
                    SecondHandHouse bean = (SecondHandHouse) JsonUtil.jsonToObject(o.toString(),c);
                    secondHandHouseList.add(bean);
                }else if (catid.equals("64")){
                    SecondHandCar bean = (SecondHandCar) JsonUtil.jsonToObject(o.toString(),c);
                    secondHandCarList.add(bean);
                }else if (catid.equals("16")){
                    PlayBean bean = (PlayBean) JsonUtil.jsonToObject(o.toString(),c);
                    playList.add(bean);
                }else if (catid.equals("32")){
                    PlayBean bean = (PlayBean) JsonUtil.jsonToObject(o.toString(),c);
                    playList.add(bean);
                }else if (catid.equals("49")){
                    PlayBean bean = (PlayBean) JsonUtil.jsonToObject(o.toString(),c);
                    playList.add(bean);
                }else if (catid.equals("4")){
                    DaydayCouponBean bean = (DaydayCouponBean) JsonUtil.jsonToObject(o.toString(),c);
                    ddList.add(bean);
                }else if(catid.equals("31")){
                    FocusOnBean bean = (FocusOnBean) JsonUtil.jsonToObject(o.toString(),c);
                    focusOnBean.add(bean);
                }else if(catid.equals("53")||catid.equals("54")){
                    MyJoinBean bean = (MyJoinBean) JsonUtil.jsonToList(o.toString(),c);
                    myJoinBeans.add(bean);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_MYFAVORITES_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "PostMyFavoritesLIst.no()");

//        ArrayList 在这里不能清空
//        DataCenter.daydayCouponBeanList.clear();
//        DataCenter.auctionArrayList.clear();
//        DataCenter.messageBeanArrayList.clear();
//        DataCenter.secondHandCar.clear();
//        DataCenter.secondHandHouse.clear();
//        DataCenter.playBeanList.clear();
//        DataCenter.focusOnBeans.clear();
//        DataCenter.myJoinBeans.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_MYFAVORITES_FAILED);

    }
}
